package com.ueboot.weishop.api.controller.user;


import com.ueboot.core.exception.BusinessException;
import com.ueboot.core.http.response.Response;
import com.ueboot.core.utils.DateUtils;
import com.ueboot.weishop.api.SessionUtils;
import com.ueboot.weishop.api.configure.AuthenticationInterceptor;
import com.ueboot.weishop.api.controller.user.vo.LoginReq;
import com.ueboot.weishop.api.controller.user.vo.LoginResp;
import com.ueboot.weishop.api.controller.user.vo.ServerDateResp;
import com.ueboot.weishop.entity.customer.CustomerInfoEntity;
import com.ueboot.weishop.entity.user.FordClubMember;
import com.ueboot.weishop.entity.user.JoUser;
import com.ueboot.weishop.service.customer.CustomerService;
import com.ueboot.weishop.service.user.FordClubMemberService;
import com.ueboot.weishop.service.user.JoUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Neel on 2017/5/9.
 * 模拟用户登录，将openId存入session当中，后续所有接口都可以从session当中获取
 */

@RestController
@RequestMapping(value = "/api/public")
@Slf4j
public class UserController {

	@Autowired
	private FordClubMemberService fordClubMemberService;


	@Autowired
	private CustomerService customerService;


    @RequestMapping(value = "/loginNo", method = RequestMethod.POST)
    public Response<LoginResp> loginNo(@RequestBody LoginReq req, HttpServletRequest request) {
        if (StringUtils.isBlank(req.getOpenId())) {
            throw new BusinessException("OPENID 不存在");
        }
        SessionUtils.set(req.getOpenId());
        return new Response<>(new LoginResp());
    }

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Response<LoginResp> login(@RequestBody LoginReq req, HttpServletRequest request) {
		if (StringUtils.isBlank(req.getOpenId())) {
			throw new BusinessException("OPENID 不存在");
		}
		SessionUtils.set(req.getOpenId());

		LoginResp resp = new LoginResp();


		JoUser joUser = joUserService.findByWechatUserName(req.getOpenId());
		List<FordClubMember> memberList = fordClubMemberService.findByOpenId(req.getOpenId());
		if (joUser == null || memberList == null || memberList.size() == 0) {
			log.info("openId:{},未认证用户登录!", req.getOpenId());
			resp.setAuth(false);
			return new Response<>(resp);
		}
		FordClubMember member = memberList.get(0);
		resp.setVin(member.getVvin());
		resp.setName(member.getName());
		resp.setMobile(member.getMobile());
		//如果member对应的vin码不为空，则认为已经认证
		if (StringUtils.isNotBlank(member.getVvin())) {
			resp.setAuth(true);
		}
		resp.setUserNum(joUser.getMemberNo());
		log.info("openId:{},vin:{},mobile:{},已认证用户登录!", req.getOpenId(), member.getVvin(), member.getMobile());
		resp.setVinList(vinList(memberList));

		//查询customer对象是否存在
		CustomerInfoEntity customer = customerService.findByOpenId(req.getOpenId());
		if (customer == null) {
			customer = new CustomerInfoEntity();
			customer.setOpenId(req.getOpenId());
			customer.setName(member.getName());
			customer.setMobile(member.getMobile());
			this.customerService.save(customer);
		}
		return new Response<>(resp);
	}

	@RequestMapping(value = "/server", method = RequestMethod.POST)
	public Response<ServerDateResp> server() {
		ServerDateResp resp = new ServerDateResp();
		Date allowDate = DateUtils.parseDate("2017-11-11 08:30:00", DateUtils.FORMAT_DATE_TIME_DEFAULT);
		Date date = new Date();
		resp.setDate(DateUtils.format(date, DateUtils.FORMAT_DATE_TIME_DEFAULT));
		resp.setJoinAllow(date.after(allowDate));
		return new Response<>(resp);
	}

	private List<String> vinList(List<FordClubMember> memberList) {
		List<String> vinList = new ArrayList<>();
		for (FordClubMember member : memberList) {
			vinList.add(member.getVvin());
		}
		return vinList;
	}


}
